package com.ruoyi.central.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.cache.common.CacheConstants;
import com.ruoyi.central.publisher.PublisherUtils;
import com.ruoyi.central.publisher.event.ArtProcessEvent;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.SignEnum;
import com.ruoyi.common.utils.service.ServiceUtils;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.service.ServiceImpl;
import com.ruoyi.central.mapper.TopCentralArtProcessMapper;
import com.ruoyi.central.domain.TopCentralArtProcess;
import com.ruoyi.central.service.ITopCentralArtProcessService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.enums.common.State;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.ArrayList;

/**
 * 工艺Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-02
 */
@Service
@Slf4j
@CacheConfig(cacheNames = CacheConstants.EhcacheName.CONTINUAL_CACHE_NAME)
public class TopCentralArtProcessServiceImpl extends ServiceImpl<TopCentralArtProcessMapper,TopCentralArtProcess> implements ITopCentralArtProcessService
{
    /**
     * 查询工艺
     * 
     * @param id 工艺主键
     * @param columns 查询指定的列
     * @return 工艺
     */
    @Override
    public TopCentralArtProcess selectById(Integer id, SFunction<TopCentralArtProcess,?> ... columns){

        if(StringUtils.isNotEmpty(columns)){
            return selectOne(lambdaQuery().select(columns).eq(TopCentralArtProcess::getId,id));
        }
        return super.selectById(id);
    }

    /**
     * 查询工艺
         * 注意：只获取状态信息为可用的数据
     * 
     * @param id 工艺主键
     * @param columns 查询指定的列
     * @return 工艺
     */
    @Override
    public TopCentralArtProcess selectUsableById(Integer id, SFunction<TopCentralArtProcess,?> ... columns)
    {
            LambdaQueryWrapper<TopCentralArtProcess> lambdaQueryWrapper = lambdaQuery().eq(TopCentralArtProcess::getId,id);
            buildUsableState(lambdaQueryWrapper);
            if(StringUtils.isNotEmpty(columns)){
                lambdaQueryWrapper.select(columns);
            }
            return selectOne(lambdaQueryWrapper);
    }

    /**
     * 查询工艺列表
     *
     * @param topCentralArtProcess 工艺
     * @return 工艺
     */
    @Override
    public List<TopCentralArtProcess> selectList(TopCentralArtProcess topCentralArtProcess)
    {
        return getBaseMapper().selectTopCentralArtProcessList(topCentralArtProcess);
    }

    /**
     * 获取所有的工艺数据
     * 
     * @return Map<Integer,TopCentralArtProcess> 集合
     */
    @Cacheable
    @Override
    public Map<Integer,TopCentralArtProcess> selectAllMap(){

        return getBaseMapper().selectList(customLambdaQuery())
                .stream().collect(Collectors.toMap(TopCentralArtProcess::getId,v->v));
    }

    /**
     * 新增工艺
     *
     * @param topCentralArtProcess 工艺
     * @return 结果
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean insert(TopCentralArtProcess topCentralArtProcess)
    {
        return super.insert(topCentralArtProcess);
    }

    /**
     * 新增（批量）工艺
     *
     * @param topCentralArtProcessList 实体对象集合
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean insertBatch(Collection<TopCentralArtProcess> topCentralArtProcessList, int batchSize) {
        return super.insertBatch(topCentralArtProcessList, batchSize);
    }

    /**
     * 删除 工艺 根据编号
     * @param id
     * @return true 删除成功 false 删除失败
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean deleteById(Serializable id) {
        return super.deleteById(id);
    }

    /**
     * 批量删除 工艺 根据 编号集合
     * @param ids
     * @return true 删除成功 false 删除失败
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteByIds(Collection<? extends Serializable> ids) {

        PublisherUtils.publishEvent(ArtProcessEvent.class, ServiceUtils.toBeanList(ids,
                TopCentralArtProcess.class,(artProcess,artProcessId)->artProcess.setId(Convert.toInt(artProcessId))), SignEnum.DELETE);

        return super.deleteByIds(ids);
    }

    /**
     * 批量修改 工艺 根据唯一编号
     * @param topCentralArtProcessList
     * @param batchSize  更新批次数量
     * @return true 修改成功 false 修改失败
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean updateBatchById(Collection<TopCentralArtProcess> topCentralArtProcessList, int batchSize) {
        return super.updateBatchById(topCentralArtProcessList, batchSize);
    }

    /**
     * 修改 工艺
     * @param  topCentralArtProcess
     * @param updateWrapper
     * @return
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean update(TopCentralArtProcess topCentralArtProcess, LambdaUpdateWrapper<TopCentralArtProcess> updateWrapper) {
        return super.update(topCentralArtProcess,updateWrapper);
    }

    /**
     * 修改工艺
     *
     * @param topCentralArtProcess 工艺
     * @return 结果
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean updateById(TopCentralArtProcess topCentralArtProcess)
    {
        return super.updateById(topCentralArtProcess);
    }

    /**
     * 修改工艺状态 根据 id 工艺
     * 
     * @param id 工艺主键
     * @param state 数据状态
     * @return 结果
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean updateStateById(Integer id, State state){

        return updateById(buildTopCentralArtProcessForUpdateState(id,state));
    }

    /**
     * 修改多个工艺状态 根据 id 工艺
     * 
     * @param ids 工艺主键
     * @param state 数据状态
     * @return 结果
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean updateStateByIds(Collection<Integer> ids, State state){

        List<TopCentralArtProcess> updateTopCentralArtProcessList = new ArrayList(ids.size());

            ids.forEach(id->updateTopCentralArtProcessList.add(buildTopCentralArtProcessForUpdateState(id,state)));

        return updateBatchById(updateTopCentralArtProcessList);
    }

    /**
     * 构建修改TopCentralArtProcess 的 修改类
     * @param id 工艺主键
     * @param state 数据状态
     * @return TopCentralArtProcess构建的修改模型
     */
    private TopCentralArtProcess buildTopCentralArtProcessForUpdateState(Integer id, State state){

        TopCentralArtProcess topCentralArtProcess = new TopCentralArtProcess();
        topCentralArtProcess.setState(state);
        topCentralArtProcess.setId(id);

        return topCentralArtProcess;
    }

}
